package com.atguigu.myzhxy.controller;

import com.atguigu.myzhxy.pojo.Grade;
import com.atguigu.myzhxy.service.GradeService;
import com.atguigu.myzhxy.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * ClassName: GradeController
 * Package: com.atguigu.myzhxy.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-01-12
 * Time: 15:41
 */

@Api(tags = "年级控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    // sms/gradeController/getGrades

    @ApiOperation("获取全部年级")
    @GetMapping("/getGrades")
    public Result getGrades(){
       List<Grade> grades =  gradeService.getGrades();
       return Result.ok(grades);
    }

    @ApiOperation("删除Grade信息")
    //单个删除 和 批量删除 deleteGrade
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(
            @ApiParam("要删除的所有的grade的JSON集合")
            @RequestBody List<Integer> ids){
        //调用服务层 进行删除
        gradeService.removeByIds(ids);
        return Result.ok();
    }

    //新增年级 和 修改
    @ApiOperation("新增或者修改grade,有id属性则修改,没有则是添加")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdateGrade(
            @ApiParam("JSON的Grade对象")
            @RequestBody Grade grade){
        //接收服务处参数

        //调用服务层方法完成增加或者修改
        gradeService.saveOrUpdate(grade);
        return Result.ok();
    }


    //查询
    // /sms/gradeController/getGrades/1/3?gradeName=%E4%88%89
    @ApiOperation("根据年纪名称模糊查询,带分页")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades( @ApiParam("分页查询的页码数") @PathVariable("pageNo") Integer pageNo,
                             @ApiParam("分页查询页大小") @PathVariable("pageSize") Integer pageSize,
                             @ApiParam("分页查询模糊匹配的名称") String gradeName
    ) {
        //分页待条件查询
        Page<Grade> page = new Page<>(pageNo,pageSize);
        //通过服务层来查询
        IPage<Grade> pageRs =  gradeService.getGradeByOpr(page,gradeName);

        //封装Result对象并返回
        return Result.ok(pageRs);
    }
}
